package com.ms.education.cms.controller;

import com.ms.education.R;
import com.ms.education.cms.entity.Banner;
import com.ms.education.cms.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-01-21
 */
@Api(description = "banner")
@RestController
@RequestMapping("/cms/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Cacheable(value = "banners")
    @GetMapping("getShowGroupBanners")
    public R getShowGroupBanners() {
        List<Banner> banners = bannerService.getShowGroupBanners();
        return R.ok().data("banners", banners);
    }

    @GetMapping("getBannersByGroupId/{groupId}")
    public R getBannersByGroupId(@PathVariable("groupId") Integer groupId) {
        List<Banner> banners = bannerService.getBannersByGroupId(groupId);
        return R.ok().data("banners", banners);
    }

    @PostMapping("updateBanners")
    public R updateBanners(@RequestBody List<Banner> banners) {
        bannerService.updateBatchById(banners);
        return R.ok();
    }

    @PutMapping("saveBanner")
    public R saveBanner(Banner banner) {
        bannerService.save(banner);
        return R.ok();
    }

    @PostMapping("deleteBannerByIds")
    public R deleteBannerById(@RequestBody Integer[] ids) {
        bannerService.removeBatchByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable("id") Integer id) {
        Banner banner = bannerService.getById(id);
        return R.ok().data("banner", banner);
    }

    @ApiOperation("同步对正在展示的banner")
    @PostMapping("refreshChangedShowBanner")
    @CacheEvict(value = "banners",allEntries = true)
    public R refreshChangedShowBanner() {
        return R.ok();
    }


}
